package com.ludaxu.google.fragment;

import java.util.List;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.ListView;

import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.ludaxu.google.R;
import com.ludaxu.google.adapter.ListBaseAdapter;
import com.ludaxu.google.domain.AppInfo;
import com.ludaxu.google.holder.HomePictureHolder;
import com.ludaxu.google.protocol.HomeProtocol;
import com.ludaxu.google.tools.UiUtils;
import com.ludaxu.google.view.LoadingPage.loadResult;

public class HomeFragment extends BaseFragment {
	private List<AppInfo> datas;
	private List<String> pictures;// 顶部viewpager显示图片的数据

	// 当Fragment挂载Activity的时候调用
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		show();
	}

	/*
	 * 创建成功界面
	 */
	public View createSuccessView() {
		ListView listView = new ListView(UiUtils.getContext());

		HomePictureHolder holder = new HomePictureHolder();
		holder.setInfo(pictures);
		View contentView = holder.getContentView();// 得到holder管理的view对象
		contentView.setLayoutParams(new AbsListView.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		listView.addHeaderView(contentView);// 把holder里的view对象 添加到list中

		listView.setAdapter(new ListBaseAdapter(datas) {

			@Override
			protected List<AppInfo> onLoad() {
				HomeProtocol protocol = new HomeProtocol();
				List<AppInfo> load = protocol.load(datas.size());
				datas.addAll(load);
				return load;
			}

		});

		// 设置慢滑时加载.快速滑动时不加载图片
		listView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils,
				false, true));
		bitmapUtils.configDefaultLoadingImage(R.drawable.ic_default);// 设置加载中的图片
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.ic_default);// 设置加载失败的图片

		return listView;
	}

	// 请求服务器,返回一个结果
	public loadResult load() {
		HomeProtocol protocol = new HomeProtocol();
		datas = protocol.load(0);
		pictures = protocol.getPictures();
		// for (AppInfo appInfo : datas) {
		// System.out.println(appInfo.toString());
		// }
		return checkData(datas);
	}

}
